package cn.itcast.travel.service.impl;

import cn.itcast.travel.domain.blog;
import cn.itcast.travel.service.BlogService;
import cn.itcast.travel.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class BlogServiceImpl implements BlogService {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    //获取所有博客
    @Override
    public List<blog> getallblog() {
        List<blog> bgs=null;
        try{
            String sql="select *,t_blog.id bgid from t_blog join t_blogtype tb on tb.id = t_blog.typeId join t_blogger t on t.id = t_blog.bloggerid order by releaseDate desc;";
            bgs=template.query(sql,new BeanPropertyRowMapper<blog>(blog.class));
        }catch (Exception e){

        }
        return bgs;
    }

    //获取博客详情
    @Override
    public blog getdetail(String id) {
        blog b=null;
        try{
            String sql="select *,t_blog.id bgid from t_blog join t_blogger tb on tb.id = t_blog.bloggerid where t_blog.id=?";
            b=template.queryForObject(sql,new BeanPropertyRowMapper<blog>(blog.class),id);
        }catch (Exception e){

        }
        return b;
    }

    //增加点击量
    @Override
    public void addclick(String id) {
        String sql="update t_blog set clickHit=clickHit+1 where id=?";
        template.update(sql,id);
    }

    //添加博客
    @Override
    public void addblog(blog b) {
        String sql="insert into t_blog(bloggerid,clickHit, pic, releasedate, replyhit, summary, title, typeId) value(?,?,?,?,?,?,?,?)";
        template.update(sql,b.getBloggerid(),b.getClickHit(),b.getPic(),b.getReleaseDate(),0,b.getSummary(),b.getTitle(), b.getTypeId());
    }
}
